package com.crazy.crazybusinessservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crazy.crazybusinessservice.VO.AccountVO;
import com.crazy.crazybusinessservice.entity.Customer;
import com.crazy.crazybusinessservice.service.AccountService;
import com.crazy.crazycommon.common.BaseResponse;
import com.crazy.crazycommon.common.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Account")
public class AccountController {

    @Autowired
    public AccountService accountService;


    @GetMapping("findAll")
    public BaseResponse<List> getAllAccounts(){
        //调用service方法实现查询
        //List<Customer> list=accountService.list(null);
        List<Map<String, Object>> list=accountService.findAll();
        return ResultUtils.success(list);
    }


    //多条件组合查询并分页
    @PostMapping("pageAccount/{current}/{limit}")
    public BaseResponse<Map> pageAccountCondition(
            @PathVariable Long current,
            @PathVariable Long limit,
            @RequestBody(required = true) AccountVO accountVO){
        //构建page对象
        Page<Customer> pageParam = new Page<>(current, limit);
        //wrapper
        QueryWrapper<Customer> wrapper=new QueryWrapper<>();
        //判断条件是否为空
        String appliciationId=accountVO.getAppliciationID();
        String name=accountVO.getCustomerName();
        String openTime=accountVO.getAccountOpenDate();
        String customerType=accountVO.getCustomerType();
        int customerID=accountVO.getCustomerID();
        Date begin=accountVO.getBegin();
        Date end=accountVO.getEnd();


        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }

        if(!StringUtils.isEmpty(openTime)){
            wrapper.like("openTime",openTime);
        }

        if(!StringUtils.isEmpty(appliciationId)){
            wrapper.like("applicationID",appliciationId);
        }

        if(!StringUtils.isEmpty(customerType)){
            wrapper.like("customerType",customerType);
        }

        if(customerID != -1){
            wrapper.eq("idNumber", customerID);
        }

        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("openTime",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("openTime",end);
        }


        //调用方法
        accountService.page(pageParam, wrapper);

        List<Customer> records = pageParam.getRecords();
        long total = pageParam.getTotal();

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("records", records);
        resultMap.put("total", total);

        return ResultUtils.success(resultMap);

    }





}
